package com.loong.system.business;

import com.loong.common.mybatis.business.AbstractBusiness;
import com.loong.system.common.backend.param.SystemLetterTemplateQueryParam;
import com.loong.system.model.SystemLetterTemplate;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author LiuQun
 * @date 2020-09-28 9:11
 */
@Repository
public class SystemLetterTemplateBusiness extends AbstractBusiness<SystemLetterTemplate> {

    /**
     * 查询系统的站内信：
     *
     * @param param 若满足条件，则根据时间、内容、用户类型进行多条件查询；若不满足，则普通查询
     * @return
     */
    public List<SystemLetterTemplate> findLetterTemplateByParam(SystemLetterTemplateQueryParam param) {
        Condition condition = new Condition(SystemLetterTemplate.class);
        Example.Criteria criteria = condition.createCriteria();
        if (ObjectUtils.isNotEmpty(param.getStartTime())) {
            //指定查询的起始时间
            criteria.andGreaterThanOrEqualTo("sendTime", param.getStartTime());
        }
        if (ObjectUtils.isNotEmpty(param.getEndTime())) {
            //指定查询的结束时间
            criteria.andLessThanOrEqualTo("sendTime", param.getEndTime());
        }
        if (ObjectUtils.isNotEmpty(param.getTitle())) {
            //指定标题为模糊查询的条件
            criteria.andLike("title", "%" + param.getTitle() + "%");
        }
        if (ObjectUtils.isNotEmpty(param.getUserType())) {
            //指定用户类型为查询条件
            criteria.andEqualTo("userType", param.getUserType());
        }
        if (ObjectUtils.isNotEmpty(param.getVipLevel())){
            //vip等级
            criteria.andEqualTo("vipLevel",param.getVipLevel());
        }
        if (ObjectUtils.isNotEmpty(param.getUserLevel())){
            //用户类型
            criteria.andEqualTo("userLevel",param.getUserLevel());
        }
        if (ObjectUtils.isNotEmpty(param.getSender())){
            //发送人
            criteria.andLike("sender","%"+param.getSender()+"%");
        }
        return findByCondition(condition);
    }


}
